package com.lovetransferjia.chatuidemo.jia.utility;

import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.style.URLSpan;
import android.util.Log;

import com.lovetransferjia.chatuidemo.MyApplication;
import com.renn.rennsdk.http.HttpRequest;

public class Utility {
	static public String RemoveSlash(String str) {
		// return str.replaceAll("\\\\","");
		String aString;
		if (str.length() > 25)
			aString = "http://" + str.substring(15).replaceAll("\\\\\\\\", "");
		else {
			aString = str;
		}
		return aString;
	}
/*
	static public void addResource(HashMap<String, Integer> map) {
		map.put("w1", R.drawable.w1);
		map.put("w2", R.drawable.w2);
		map.put("w3", R.drawable.w3);
		map.put("w4", R.drawable.w4);
		map.put("w5", R.drawable.w5);
		map.put("w6", R.drawable.w6);
		map.put("w7", R.drawable.w7);
		map.put("w8", R.drawable.w8);
		map.put("w9", R.drawable.w9);
		map.put("w10", R.drawable.w10);
		map.put("w11", R.drawable.w11);
		map.put("w12", R.drawable.w12);
		map.put("w13", R.drawable.w13);
		map.put("w14", R.drawable.w14);
		map.put("w15", R.drawable.w15);
		map.put("w16", R.drawable.w16);
		map.put("w17", R.drawable.w17);
		map.put("w18", R.drawable.w18);
		map.put("w19", R.drawable.w19);
		map.put("w20", R.drawable.w20);
		map.put("w21", R.drawable.w21);
		map.put("w22", R.drawable.w22);
		map.put("w23", R.drawable.w23);
		map.put("w24", R.drawable.w24);
		map.put("w25", R.drawable.w25);
		
		
		map.put("b1", R.drawable.b1);
		map.put("b2", R.drawable.b2);
		map.put("b3", R.drawable.b3);
		map.put("b4", R.drawable.b4);
		map.put("b5", R.drawable.b5);
		map.put("b6", R.drawable.b6);
		map.put("b7", R.drawable.b7);
		map.put("b8", R.drawable.b8);
		map.put("b9", R.drawable.b9);
		map.put("b10", R.drawable.b10);
		map.put("b11", R.drawable.b11);
	
	

	}
	*/

	static public String StringFilter(String str) {
		String regEx = "[`~!@#$%^&*()+=|{}':;',//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’、？\n\t]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	}

	static public JSONArray GetArray(String urlString, String failString) {
		try {
			HttpGet httpRequest = new HttpGet(urlString);

			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse httpResponse = httpclient.execute(httpRequest,
					MyApplication.localContext);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String strResult = EntityUtils.toString(httpResponse
						.getEntity());
				Log.w("GetArray", strResult);
				if (strResult.length() > 2) {
					JSONArray jarray = new JSONArray(strResult);
					return jarray;
				}
			}
			failString = "no data";
			httpRequest.abort();
		} catch (ClientProtocolException e) {
			Log.w("login", "protocal" + e.toString());
			failString = e.toString();
		} catch (IOException e) {
			Log.w("login", "io" + e.toString());
			failString = e.toString();
		} catch (JSONException e) {
			Log.w("login", "json" + e.toString());
			failString = e.toString();
		} catch (Exception e) {
			Log.w("login", "exp" + e.toString());
			failString = e.toString();
		}
		
		return null;

	}

	static public JSONObject PostSimple(String urlString,
			List<NameValuePair> params, String statusString,String failString) {
		
		try {
			HttpPost httpRequest = new HttpPost(urlString);

			HttpEntity httpentity = new UrlEncodedFormEntity(params, "utf-8");
			httpRequest.setEntity(httpentity);
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse httpResponse = httpclient.execute(httpRequest,
					MyApplication.localContext);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				// 取得返回的字符串
				String strResult = EntityUtils.toString(httpResponse
						.getEntity());
				Log.d("what.reply", strResult);
				JSONObject jhead = new JSONObject(strResult);
				return jhead;
			} else {
				Log.d("login", "wrong status");
				failString = "服务器出现故障";
			}
		} catch (ClientProtocolException e) {
			Log.w("login", "protocal" + e.toString());
			failString = e.toString();
		} catch (IOException e) {
			Log.w("login", "io" + e.toString());
			failString = e.toString();
		} catch (Exception e) {
			Log.w("login", "exp" + e.toString());
			failString = e.toString();
		}
		return null;

	}

	static public JSONObject GETObject(String urlString, String failString) {
		try {
			HttpGet httpRequest = new HttpGet(urlString);

			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse httpResponse = httpclient.execute(httpRequest,
					MyApplication.localContext);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String strResult = EntityUtils.toString(httpResponse
						.getEntity());
				Log.w("GetObject", strResult);
				if (strResult.length() > 2) {
					JSONObject jarray = new JSONObject(strResult);
					return jarray;
				}
			}else{
				Log.w("GetObject", "网络码"+httpResponse.getStatusLine().getStatusCode());
			}
			failString = "no data";
		} catch (ClientProtocolException e) {
			Log.w("GetObject", "protocal" + e.toString());
			failString = e.toString();
		} catch (IOException e) {
			Log.w("GetObject", "io" + e.toString());
			failString = e.toString();
		} catch (JSONException e) {
			Log.w("GetObject", "json" + e.toString());
			failString = e.toString();
		} catch (Exception e) {
			Log.w("GetObject", "exp" + e.toString());
			failString = e.toString();
		}
		Log.w("GetObject", "return null");
		return null;

	}
	
	static public Boolean GetUserinfo(int id ){
		String urlString=MyApplication.baseUrl+"user_info/info/"+id+"/0"+MyApplication.postfix;
		String failString="";
		try {
			JSONObject jsonObject = GETObject(urlString, failString);
			MyApplication.id = jsonObject.getString("id");
			MyApplication.name = jsonObject.getString("name");
			MyApplication.school = jsonObject.getString("school");
			MyApplication.major= jsonObject.getString("major");
			MyApplication.profile= jsonObject.getString("profile");
			MyApplication.hobby= jsonObject.getString("hobby");
			MyApplication.gender= jsonObject.getString("gender");
			MyApplication.city= jsonObject.getString("city");
			MyApplication.headimage= jsonObject.getString("headimage");
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}
}
